package pl.edu.agh.mguesser.game.answer;

import pl.edu.agh.mguesser.exception.MusicGuesserRuntimeException;
import pl.edu.agh.mguesser.game.listener.AnswerRequiredListener;
import pl.edu.agh.mguesser.game.listener.SongRevealedListener;
import pl.edu.agh.mguesser.metadata.Metadata;
import pl.edu.agh.mguesser.metadata.MetadataProvider;
import pl.edu.agh.mguesser.metadata.MetadataProviderException;
import pl.edu.agh.mguesser.player.Player;

/**
 * @author keiran
 */
public class AnswerRequiredHandler {
    
    private AnswerRequiredListener answerRequiredListener;
    private SongRevealedListener songRevealedListener;
    private MetadataProvider metadataProvider;
    private Player player;

    public AnswerRequiredHandler(AnswerRequiredListener answerRequiredListener,
            SongRevealedListener songRevealedListener, MetadataProvider metadataProvider, Player player) {
        this.answerRequiredListener = answerRequiredListener;
        this.songRevealedListener = songRevealedListener;
        this.metadataProvider = metadataProvider;
        this.player = player;
    }
    
    public Answer getAnswer() {
        try {
            Metadata metadata = metadataProvider.getMetadataFor(player.getCurrent());
            songRevealedListener.songRevealed(metadata);
            return answerRequiredListener.getAnswer();
            
        } catch(MetadataProviderException ex) {
            throw new MusicGuesserRuntimeException(ex);
            
        } finally {
            songRevealedListener.userAnswered();
            
        }
    }

    AnswerRequiredListener getAnswerRequiredListener() {
        return answerRequiredListener;
    }

    SongRevealedListener getSongRevealedListener() {
        return songRevealedListener;
    }

    MetadataProvider getMetadataProvider() {
        return metadataProvider;
    }

    Player getPlayer() {
        return player;
    }
    
}
